#ifndef TAB_HPP
#define TAB_HPP

#include "editorindicatorsborder.h"
#include "indicatorsborder.h"
#include "vtextedit.h"

#include <QWidget>
#include <qboxlayout.h>
#include <qlistwidget.h>
#include <qscrollbar.h>
#include <QDebug>
#include <QAbstractTextDocumentLayout>


class tab : public QWidget
{
    Q_OBJECT
public:
    tab(QWidget *parent = nullptr):QWidget(parent) {


        auto mainLayout = new QHBoxLayout(this);
        mainLayout->setContentsMargins(0, 0, 0, 0);
        mainLayout->setSpacing(0);

        m_textEdit = new VTextEdit(this);
        m_textEdit->setAcceptRichText(false);
        m_textEdit->setSizePolicy(QSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Expanding));
        // We don't need its border.
        m_textEdit->setFrameStyle(QFrame::NoFrame);
        setFocusProxy(m_textEdit);


        m_indicatorsBorderInterface.reset(new EditorIndicatorsBorder(m_textEdit));
        m_indicatorsBorder = new IndicatorsBorder(m_indicatorsBorderInterface.data(),
                                                  IndicatorsBorder::LineNumberType::Absolute,
                                                  true,
                                                  this);
        m_indicatorsBorder->setSizePolicy(QSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Ignored));
//        m_indicatorsBorder->setBackgroundColor(QColor(255, 255, 255));
        m_indicatorsBorder->resize(100, this->height());
        connect(m_textEdit, &VTextEdit::cursorLineChanged,
                m_indicatorsBorder, &IndicatorsBorder::updateBorder);

        auto sb = m_textEdit->verticalScrollBar();
        if (sb) {
            connect(m_textEdit->verticalScrollBar(), &QScrollBar::valueChanged,
                    m_indicatorsBorder, &IndicatorsBorder::updateBorder);
        }

        connect(m_textEdit, &VTextEdit::contentsChanged,
                m_indicatorsBorder, &IndicatorsBorder::updateBorder);

        connect(m_textEdit, &VTextEdit::resized,
                m_indicatorsBorder, &IndicatorsBorder::updateBorder);


        mainLayout->addWidget(m_indicatorsBorder);
        mainLayout->addWidget(m_textEdit);


        m_indicatorsBorderInterface->m_folding = new TextFolding(m_textEdit->document());
        connect(m_indicatorsBorderInterface->m_folding, &TextFolding::foldingRangesChanged,
                m_indicatorsBorder, &IndicatorsBorder::updateBorder);

        connect(m_textEdit->document(), SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNums(int)));
    }
    virtual ~tab() {}

public:
    int block_count = 0;

    VTextEdit *m_textEdit = nullptr;

    IndicatorsBorder *m_indicatorsBorder = nullptr;

    QScopedPointer<EditorIndicatorsBorder> m_indicatorsBorderInterface;
};



#endif // TAB_HPP
